exports.TrashHandler = Core.Class.singleton(
{
    classname: 'TrashHandler',
    
    initialize: function() 
    {
    	this._trashUIArray = null;
		this._checkForTrashTimer = null;
		
    	this._trashUIArray = new GAMutableArray();	
    },
    
    initializeAllTrashes: function()
    {
    	this.adjustTrashCountForFirstTimeUser();
    	var trashToBeProduced = this.getTotalTrashToBeProducedCount();
    	
    	for(var i=0 ; i < trashToBeProduced; i++)
    	{
			var trashUI = new TrashUI();
			//var floor = i;
			trashUI.createView();
	    	if(!TutorialManager.getHasCompletedTutorial() && i == 0)
	    	{
	    		trashUI.setPosition(TCUtil.getAdjustedX(120), TCUtil.getAdjustedY(-35));	
	    	}
			else
			{
				trashUI.setPosition(this.getRandomTrashPosition());
			}
			trashUI.setDepth(99997);
			MMGameLayer.getMallUI().addChild(trashUI);
			
			this._trashUIArray.addObject(trashUI);
    	}
    },
    
    initNewTrashTimer: function()
    {
    	this._checkForTrashTimer = setInterval(this.checkAndAddTrash.bind(this), 30000);
    },
    
    addTrash: function()
    {
		if(this._trashUIArray)
		{		
			//console.log("zaki::: add TRASH CALLED ");
			var trashUI = new TrashUI();
			//var floor = this.TEMP++;
			trashUI.createView();
			//trashUI.setPosition([TCUtil.getAdjustedX(200), TCUtil.getAdjustedY(-(40 + (floor) * FLOOR_HEIGHT))]);
			
			trashUI.setPosition(this.getRandomAvatarPosition());
			trashUI.setDepth(99997);
			MMGameLayer.getMallUI().addChild(trashUI);
			
			this._trashUIArray.addObject(trashUI);
		}
    },
    
    getRandomTrashPosition: function()
    {
		var positionX = null;
    	var noOfFloors = MMGameLayer.getMallUI().getFloorsUIArray().getLength();
    	var randomFloorNumber = Math.round(Math.random()*(noOfFloors-1));
    	var positionY = TCUtil.getAdjustedY(-(35 + (randomFloorNumber) * FLOOR_HEIGHT));

		positionX = TCUtil.getAdjustedX(Math.round(Math.random()*320));
    	var position = new Core.Vector(positionX, positionY);
    	return position;
    },
    
    getTotalTrashToBeProducedCount: function()
    {
    	var mallUI = MMGameLayer.getMallUI();
    	var noOfStores = mallUI.getStoresUIArray().getLength();
    	var mallModel = mallUI.getMallObj();
    	var lastTrashCollectedTime = mallModel.getLastTrashCollectedTime();
    	var currentTime = MMGameState.getServerTime();
    	
    	var trashToBeProducedCount = Math.ceil(noOfStores * TRASH_PER_STORE_PER_HOUR * ((currentTime - lastTrashCollectedTime) /3600));
    	if(trashToBeProducedCount > MAX_TRASH_COUNT)
    	{
    		trashToBeProducedCount = MAX_TRASH_COUNT;
    	}
    	return trashToBeProducedCount;
    },
    
	showTutorialForTrash: function()
	{
		this._tutorialIndicatorForTrash = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForTrash.setPosition([TCUtil.getAdjustedX(140), TCUtil.getAdjustedY(390)]);
		this._tutorialIndicatorForTrash.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForTrash);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(90),TCUtil.getAdjustedY(180)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_11"));
	},
	
	showTutorialForFriendTrash: function()
	{
		this._tutorialIndicatorForFriendTrash = new SwingAnimation(kSwingDirection.kSwingDirectionLeft);
		this._tutorialIndicatorForFriendTrash.setPosition([TCUtil.getAdjustedX(150), TCUtil.getAdjustedY(390)]);
		this._tutorialIndicatorForFriendTrash.createView();
		NGWindow.document.addChild(this._tutorialIndicatorForFriendTrash);
		TutorialManager.getTutorialPopup().setCurrentPosition([TCUtil.getAdjustedX(90),TCUtil.getAdjustedY(180)]);
		TutorialManager.getTutorialPopup().updateText(Core.Localization.getString("TUTORIAL_TEXT_12_E"));
	},
    
	adjustTrashCountForFirstTimeUser: function()
	{
    	var mallUI = MMGameLayer.getMallUI();
    	var mallModel = mallUI.getMallObj();
    	var currentTime = MMGameState.getServerTime();
    	var lastTrashCollectedTime = mallModel.getLastTrashCollectedTime();
    	if(lastTrashCollectedTime == 0)
    	{
    		
    		mallModel.setLastTrashCollectedTime(currentTime - 3600);
    		mallModel.updateModel();
    	} 
	},
	
	checkAndAddTrash: function()
	{
		var trashToBeProduced = this.getTotalTrashToBeProducedCount();
		var trashAlreadyPresent = this._trashUIArray.getLength();
		var trashNeededCount = trashToBeProduced - trashAlreadyPresent;
		//console.log("zaki::: trashCountNEeded is "+trashNeededCount);
		if(trashNeededCount > 0)
		{
			this.addTrash();
		}
	},
	
	trashTappedForRemoval: function(trashUI)
	{
		if(!TutorialManager.getHasCompletedTutorial())
		{
			if(this._tutorialIndicatorForTrash)
			{
				this._tutorialIndicatorForTrash.destroy();
				this._tutorialIndicatorForTrash = null;
				TutorialManager.showNextTutorialStep();
			}
			if(this._tutorialIndicatorForFriendTrash)
			{
				this._tutorialIndicatorForFriendTrash.destroy();
				this._tutorialIndicatorForFriendTrash = null;
				MMGameLayer.getControlLayer().getBottomBar().showTutorialForStealing();
			}
		}
		var mallUI = MMGameLayer.getMallUI();
    	var mallModel = mallUI.getMallObj();
    	var noOfStores = mallUI.getStoresUIArray().getLength();
    	var currentTime = MMGameState.getServerTime();
		
		var currentTrashCount = this._trashUIArray.getLength();
		
		var difference = ((currentTrashCount-1)/(noOfStores * TRASH_PER_STORE_PER_HOUR))*3600;
		
		var newLastCollectedTime = currentTime - difference;
		
		mallModel.setLastTrashCollectedTime(newLastCollectedTime);
		//user.updateModel();no need here as long as rewardOnTrashRemoval function is in the end
		this.rewardOnTrashRemoval();
		
    	this._trashUIArray.removeObject(trashUI);
    	trashUI.destroy();
	},
	

	// i am sure this function should not be here
	getRandomAvatarPosition: function()
	{	
		var avatarUIArray = AvatarHandler.getAvatarsUIArray();		
		var noOfAvatars = avatarUIArray.getLength();
		var randomKey = Math.round(Math.random() * (noOfAvatars-1));
		//console.log("zaki::: trashs randomKey start "+randomKey);
		
		for(var i = 0 ; i < noOfAvatars ; i++,randomKey++)
		{
			//console.log("zaki::: trashs randomKey before "+randomKey);
			randomKey = randomKey % noOfAvatars;
			//console.log('zaki::: trashs randomKey after'+randomKey);			
			var avatarUI = avatarUIArray.objectAtIndex(randomKey);
			//console.log("zaki::: trash avatarUI is "+avatarUI);
			if(avatarUI && avatarUI.getVisible())
			{
				var avatarPosition = avatarUI.getPosition();
				return avatarPosition;
			}
		}
	},
	clearAddTrashTimer: function()
	{
		if(this._checkForTrashTimer)
		{
			clearTimeout(this._checkForTrashTimer);
			this._checkForTrashTimer = null;
		}
	},
	
	rewardOnTrashRemoval: function()
	{
		MMGameState.getCurrentUser().updateCurrency(kOperationType.kOperationAddCoins,TRASH_COLLECT_REWARD);
	},
       
    getTrashUIArray: function()
    {
    	return this._trashUIArray;
    },
    
    destroyTrashUIObjects: function()
    {
    	var count = this._trashUIArray.getLength();
    	for(var i = 0; i < count;)
    	{
    		this._trashUIArray.objectAtIndex(i).destroy();
    		this._trashUIArray.removeObjectAtIndex(i);
    		count--;
    	}
    },
	
	getBaseResourcePath: function()
	{
		return "./Content/images/gamelayer/environmentlayer/trashimages/";
	},
								   
	destroy: function()
    {
    	
    },
});
